<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索框 -->
    <el-card class="box-card">
      <el-input
        placeholder="请输入内容"
        v-model="queryinfo.query"
        clearable
        class="serch"
      ></el-input>
      <el-button icon="el-icon-search" border="0" @click="shopCommtion"
        >搜索</el-button
      >
      <!-- 添加商品按钮 -->
      <el-button type="primary" @click="dialogShopAdd = true"
        >添加商品</el-button
      >
    </el-card>
    <!-- 商品渲染 -->
    <el-table :data="shopData" border style="width: 100%">
      <el-table-column prop="goods_id" label="#" width="50"> </el-table-column>
      <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
      <el-table-column prop="goods_price" label="商品价格（元）" width="120">
      </el-table-column>
      <el-table-column prop="goods_weight" label="商品重量" width="120">
      </el-table-column>
      <el-table-column prop="add_time" label="创建时间" width="170">
        <template slot-scope="scope">
          {{ scope.row.add_time | newFromTime }}
        </template>
      </el-table-column>
      <el-table-column prop="" label="操作" width="200">
        <template slot-scope="scope">
          <el-row>
            <!-- c根据id查询 -->
            <el-button size="small" @click="searchGoods(scope.row)"
              ><i class="el-icon-search"></i
            ></el-button>
            <!-- 修改商品按钮 -->
            <el-button
              type="primary"
              size="small"
              @click="changeGoods(scope.row)"
              ><i class="el-icon-edit"></i
            ></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" size="small" @click="deleteBtn(scope.row)"
              ><i class="el-icon-delete"></i
            ></el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页按钮 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryinfo.pagenum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryinfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 添加商品弹窗 -->
    <el-dialog title="添加商品" :visible.sync="dialogShopAdd">
      <el-form :model="addShopping">
        <el-form-item label="商品名称">
          <el-input
            v-model="addShopping.goods_name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="三级排列">
          <el-input
            v-model="addShopping.goods_cat"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input
            v-model="addShopping.goods_price"
            autocomplete="off"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input
            v-model="addShopping.goods_number"
            autocomplete="off"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="重量">
          <el-input
            v-model="addShopping.goods_weight"
            autocomplete="off"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="介绍">
          <el-input
            v-model="addShopping.goods_introduce"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogShopAdd = false">取 消</el-button>
        <el-button type="primary" @click="addGoods">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 查询指定id信息弹窗 -->
    <el-dialog title="商品详情" :visible.sync="dialogSerchGood">
      <el-form :model="GoodsDetails">
        <el-form-item label="商品ID">
          <el-input
            v-model="GoodsDetails.goods_id"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input
            v-model="GoodsDetails.goods_name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input
            v-model="GoodsDetails.goods_price"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input
            v-model="GoodsDetails.goods_number"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="重量">
          <el-input
            v-model="GoodsDetails.goods_weight"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogSerchGood = false">取 消</el-button>
        <el-button type="primary" @click="dialogSerchGood = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 编辑商品信息 -->
    <el-dialog title="编辑商品" :visible.sync="dialogChangeGood">
      <el-form :model="changeGoodData">
        <el-form-item label="商品ID">
          <el-input
            v-model="changeGoodData.goods_id"
            autocomplete="off"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input
            v-model="changeGoodData.goods_name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input
            v-model="changeGoodData.goods_price"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input
            v-model="changeGoodData.goods_number"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="重量">
          <el-input
            v-model="changeGoodData.goods_weight"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="介绍">
          <el-input
            v-model="changeGoodData.goods_introduce"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogChangeGood = false">取 消</el-button>
        <el-button type="primary" @click="okChangeGoods">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "VueGoods",

  data() {
    return {
      //商品遍历
      shopData: [],
      queryinfo: {
        //查询参数
        query: "",
        //当前页
        pagenum: 1,
        //每页条数
        pagesize: 10,
      },
      //总数
      total: 0,
      //添加商品弹窗
      dialogShopAdd: false,
      //商品遍历数组
      addShopping: {
        goods_name: "",
        goods_cat: "",
        goods_price: 0,
        goods_number: 0,
        goods_weight: 0,
        goods_introduce: "",
      },
      //商品详情弹窗
      dialogSerchGood: false,
      //详情遍历
      GoodsDetails: {},
      //编辑弹窗
      dialogChangeGood: false,
      //编辑编辑对象
      changeGoodData: {},
      //新对象数组
      totalData: {},
    };
  },
  methods: {
    //封装函数
    async shopCommtion() {
      //发送请求
      var { data } = await this.$axios.get("goods", { params: this.queryinfo });
      //   console.log(data);
      if (data.meta.status == 200) {
        this.shopData = data.data.goods;
        this.total = data.data.total;
      } else {
        this.$message({
          type: "error",
          message: data.meta.msg,
        });
      }
    },
    //pageSize 改变时会触发
    handleSizeChange(newsize) {
      this.queryinfo.pagesize = newsize;
      this.shopCommtion();
    },
    // currentPage 改变时会触发
    handleCurrentChange(newpage) {
      //页面发生变化时触发
      this.queryinfo.pagenum = newpage;
      this.shopCommtion();
    },
    //添加用户
    async addGoods() {
      var { data } = await this.$axios.post("/goods", this.addShopping);
      console.log(data);
      if (data.meta.status == 201) {
        this.$message({
          type: "success",
          message: data.meta.msg,
        });
        this.dialogShopAdd = false;
        this.shopCommtion();
      } else {
        this.$message({
          type: "error",
          message: data.meta.msg,
        });
      }
    },
    //查询商品详情
    async searchGoods(shopinfo) {
      // 显示弹窗
      this.dialogSerchGood = true;
      //发送请求
      var { data } = await this.$axios.get("goods/" + shopinfo.goods_id);
      // console.log(data);
      this.GoodsDetails = data.data;
      console.log(this.GoodsDetails);
    },

    //编辑商品信息
    async changeGoods(goodshop) {
      this.dialogChangeGood = true;
      this.changeGoodData = goodshop;
      //发送请求
      // var { data } = await this.$axios.get(
      //   "/goods/" + this.changeGoodData.goods_id
      // );
      // console.log(data);
      // //存储
      // this.totalData = data.data;
    },
    //确定编辑商品信息
    async okChangeGoods() {
      // 将新的数据整合之后传递
      var { data } = await this.$axios.get(
        "/goods/" + this.changeGoodData.goods_id
      );
      this.changeGoodData = Object.assign(data.data, this.changeGoodData);
      var { data } = await this.$axios.put(
        "/goods/" + this.changeGoodData.goods_id,
        this.changeGoodData
      );
      // console.log(data);
      if (data.meta.status == 200) {
        this.$message({
          type: "success",
          message: data.meta.msg,
        });
        this.dialogChangeGood = false;
      } else {
        this.$message({
          type: "error",
          message: data.meta.msg,
        });
      }
    },
    //删除商品
    deleteBtn(shopinfo) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          var { data } = await this.$axios.delete(
            "/goods/" + shopinfo.goods_id
          );
          if (data.meta.status == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.shopCommtion();
          } else {
            this.$message({
              type: "error",
              message: data.meta.msg,
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  mounted() {
    this.shopCommtion();
  },
};
</script>

<style>
.el-dialog__header {
  line-height: 0px;
}
.dialog-footer {
  line-height: 0px;
}
.has-gutter {
  line-height: 35px;
}
.el-row {
  line-height: 60px;
}
.el-card__body {
  line-height: 0px;
}

.serch {
  width: 22vw !important;
}
.el-input {
  width: 45vw;
}
</style>